﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZLCMS.Business;
using System.Text;
using NHibernate.Criterion;
using NHibernate;
using System.Collections;
using ZLCMS.Data.Model;
using ZLCMS.Business.DwzFrame;
using ZLCMS.Business.Config;
using Newtonsoft.Json;
using ZLCMS.Business.Util;

namespace ZLCMS.Web.SystemManage.Weixin
{
    public partial class SelectMaterialList : System.Web.UI.Page
    {
        protected FrameHelper _framePage;

        public string _s_title;
        public string _s_ddltype;

        protected string _userAccount;

        protected void Page_Load(object sender, EventArgs e)
        {
            _userAccount = UserKit.GetUserAccountFailLogin();

            //可以由其他页面传递查询参数
            if (!IsPostBack)
            {
                tb_search_title.Text = Request["title"];
            }

            //初始化全局变量值
            _s_title = tb_search_title.Text;
            _s_ddltype = ddl_type.SelectedValue;

            ////初始化控件值
            ParamKit.Bind("WXMaterialType", ddl_type, null, new List<ListItem>() { new ListItem() { Text = "营销素材", Value = "interaction" }, ParamKit.ParamItemEx_QXZ() }, null, "-1", "-1");
            ddl_type.SelectedValue = _s_ddltype;

            tb_search_title.Text = _s_title;

            //初始化DWZ分页控件
            _framePage = new FrameHelper(this) { RecordCount = GetListCount() };

            BindList(_framePage.PageIndex, _framePage.PageSize);
        }

        /// <summary>
        /// 填充网格数据
        /// </summary>
        /// <param name="pageIndex">分页索引</param>
        /// <param name="pageSize">分页大小</param>
        protected void BindList(int pageIndex, int pageSize)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxMaterial>();
                SetupCondition(ref criteria);
                criteria.AddOrder(Order.Desc("Id")).SetFirstResult((pageIndex - 1) * pageSize).SetMaxResults(pageSize);

                grid.DataSource = criteria.List();
                grid.DataBind();
            }
        }

        /// <summary>
        /// 填充查询条件
        /// </summary>
        /// <param name="criteria"></param>
        private void SetupCondition(ref ICriteria criteria)
        {
            criteria.Add(Expression.Eq("OwnerAccount", _userAccount));

            if (!string.IsNullOrEmpty(_s_title))
            {
                criteria.Add(Expression.Like("Title", "%" + _s_title + "%"));//
            }

            if (_s_ddltype.Trim() != "-1" && !string.IsNullOrEmpty(_s_ddltype))
            {
                if (_s_ddltype == "interaction")
                {
                    criteria.Add(Expression.Not(Expression.In("MsgType", new string[] { "news", "text", "video" })));
                }
                else
                {
                    criteria.Add(Expression.Eq("MsgType", _s_ddltype));//
                }
            }
        }

        /// <summary>
        /// 查询数据总数
        /// </summary>
        /// <returns></returns>
        protected int GetListCount()
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                ICriteria criteria = session.CreateCriteria<WxMaterial>();
                SetupCondition(ref criteria);
                object obj = criteria.SetProjection(Projections.RowCount()).UniqueResult();
                return int.Parse(obj.ToString());
            }
        }

        protected string GetMemberRealName(object account)
        {
            using (ISession session = DotNet.Common.NHibernateUtil.SessionFactoryHelper.CreateSession())
            {
                MemberInfo member = session.CreateCriteria<MemberInfo>().Add(Expression.Eq("AccountId", account.ToString())).UniqueResult<MemberInfo>();

                return member.MemberName;
            }
        }
    }
}
